package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    //新增菜品
    @Override
    public void save(Dish dish) {
        //1. 菜品表中新增菜品(mybatisplus保存成功后会返回保存对象的主键)
        dishMapper.insert(dish);

        //2. 新增口味表
        //2-1. 获取到该新增菜品的id,用于填充口味表中的dish_id (mybatisplus保存成功后会返回保存对象的主键)
        log.info("保存之后的菜品主键:{}", dish.getId());

        //2-2 遍历前端传入的口味列表信息
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        for (DishFlavor dishFlavor : dishFlavorList) {//name和value已经有了
            //只需设置dish_id即可
            dishFlavor.setDishId(dish.getId());
        }
    }

    //删除(单个/批量)
    @Override
    public void deleteByIds(Long[] ids) {
        //首先需要判断当前是否有菜品处于启售状态
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId, ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);

        Integer count = dishMapper.selectCount(dishLambdaQueryWrapper);
        if (count > 0) {//含有在售状态的菜品
            throw new CustomException("选中菜品中含有在售状态的菜品,禁止删除!");
        }
//        for (long id : ids) {
//            Dish dish = dishMapper.selectById(id);
//            if (dish.getStatus() == 1) {//当前菜品处于启售状态
//                throw new CustomException("选中菜品中含有在售状态的菜品,禁止删除!");
//            }
//        }
        //删除菜品及菜品口味
        for (long id : ids) {
            //删除菜品的口味
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(dishFlavorLambdaQueryWrapper);//根据dish_id批量删除菜品口味
//            Dish dish = dishMapper.selectById(id);
//            List<DishFlavor> flavorList = dish.getFlavors();
//            for (DishFlavor dishFlavor : flavorList) {
//                dishFlavorMapper.deleteById(dishFlavor.getId());
//            }
            dishMapper.deleteById(id);//删除菜品
        }
    }


    //更新菜品
    @Override
    public void update(Dish dish) {
        //1. 修改dish菜品表
        dishMapper.updateById(dish);

        //2. 修改菜品口味表
        //2-1 将原来口味表中的信息删除
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(dishFlavorLambdaQueryWrapper);
        //2-2 将新的口味信息填入口味表
        if (CollectionUtil.isNotEmpty(dish.getFlavors())) {
            for (DishFlavor flavor : dish.getFlavors()) {
                flavor.setDishId(dish.getId());//设置菜品id
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    //启售/停售(单个/批量)
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    @Override//分页查询
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1. 从dish菜品表中根据name分页查询  1-1 准备分页对象
        Page<Dish> page = new Page<>(pageNum, pageSize);//当前第pageNum页  每页pageSize条

        //1-2 封装条件查询 当name非空时,模糊查询(分页) Dish::getName  like 传进来的name
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //StringUtils.isNotEmpty(name):判断name是否为非空字符串    非空:TRUE  空:FALSE
        dishLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //selectPage 分页查询
        page = dishMapper.selectPage(page, dishLambdaQueryWrapper);//查询到的结果存入page中

        List<Dish> dishList = page.getRecords();//从page中拿到查询到的dish集合
        //CollectionUtil.isNotEmpty(dishList)判断dishList是否非空
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //2. 条件:dish菜品表的菜品分类category_id      selectById:主键查询      得到category菜品分类表
                Category category = categoryMapper.selectById(dish.getCategoryId());//根据dish菜品表的菜品分类category_id 查询到菜品分类表category
                dish.setCategoryName(category.getName());//将category的name 传给dish的菜品分类

                //3. 条件:菜品表的id    得到dishFlavor菜品口味表的口味数据
                LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());//将dish菜品表的id和dish_flavor菜品口味表的dish_id对比
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);//一个菜品可能有多个口味
                dish.setFlavors(dishFlavorList);
            }
        }
        return page;
    }

    @Override//回显菜品
    public Dish findById(Long id) {
        //dish菜品表信息
        Dish dish = dishMapper.selectById(id);

        //获取菜品分类信息
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());

        //获取菜品口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
        dish.setFlavors(dishFlavorList);
        return dish;
    }

    @Override   //通过套餐表的categoryId 查询在售菜品
    public List<Dish> findDishOnSale(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, categoryId);
        dishLambdaQueryWrapper.eq(Dish::getStatus, status);//只需要查询在售的

        List<Dish> dishList = dishMapper.selectList(dishLambdaQueryWrapper);

        //获取菜品口味信息
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());

                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
                dish.setFlavors(dishFlavorList);
            }
        }

        return dishList;
    }

    @Override    //通过name模糊查询菜品
    public List<Dish> findByName(String name) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);//只需要查询在售的
        List<Dish> dishList = dishMapper.selectList(dishLambdaQueryWrapper);
        return dishList;
    }

}
